package com.leased.service.impl;

import com.leased.exception.DatabaseException;
import com.leased.interceptor.PageMybatisInterceptor;
import com.leased.mapper.ClientUpdateInfoMapper;
import com.leased.pojo.ClientUpdateInfo;
import com.leased.pojo.ClientUpdateInfoExample;
import com.leased.request.client.ClientUpdateRequest;
import com.leased.service.ClientUpdateService;
import com.leased.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by shencx on 2017/8/16.
 */
@Service
public class ClientUpdateServiceImpl implements ClientUpdateService{

    Logger LOG = LoggerFactory.getLogger(ClientUpdateServiceImpl.class);

    @Resource
    private ClientUpdateInfoMapper clientUpdateInfoMapper;

    /**
     * 添加版本
     * @param clientUpdateInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public Integer addClientUpdateInfo(ClientUpdateInfo clientUpdateInfo) throws DatabaseException {
        try {
            if (clientUpdateInfo == null){
                LOG.error("addClientUpdateInfo 信息为空",clientUpdateInfo);
                return Integer.valueOf(0);
            }
            clientUpdateInfo.setCreateTime(new Date());
            int val = clientUpdateInfoMapper.insertSelective(clientUpdateInfo);
            if (val > 0){
                return clientUpdateInfo.getId();
            }else {
                return Integer.valueOf(0);
            }
        } catch (Throwable e) {
            LOG.error("addClientUpdateInfo exception",e,clientUpdateInfo);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 更新版本信息
     * @param clientUpdateInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public boolean updateClientInfo(ClientUpdateInfo clientUpdateInfo) throws DatabaseException {
        try {
            if (clientUpdateInfo == null){
                LOG.error("updateClientInfo 信息为空",clientUpdateInfo);
                return false;
            }
            Integer val = clientUpdateInfoMapper.updateByPrimaryKeySelective(clientUpdateInfo);
            return val>0?true:false;
        }  catch (Throwable e) {
            LOG.error("updateClientInfo exception",e,clientUpdateInfo);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取列表
     * @param clientUpdateRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<ClientUpdateInfo> fetchClientUpdateList(ClientUpdateRequest clientUpdateRequest) throws DatabaseException {
        try {
            if (clientUpdateRequest == null){
                LOG.error("fetchClientUpdateList 信息为空",clientUpdateRequest);
                return null;
            }
            ClientUpdateInfoExample example = new ClientUpdateInfoExample();
            ClientUpdateInfoExample.Criteria criteria = example.createCriteria();
            if(clientUpdateRequest.getPlatformType() != null){
                criteria.andPlatformTypeEqualTo(clientUpdateRequest.getPlatformType());
            }
            if (clientUpdateRequest.getUserType() != null){
                criteria.andUserTypeEqualTo(clientUpdateRequest.getUserType());
            }
            example.setOrderByClause("create_time desc");
            PageMybatisInterceptor.startPage(clientUpdateRequest.getPageNum(),clientUpdateRequest.getPageSize());
            clientUpdateInfoMapper.selectByExample(example);
            Page<ClientUpdateInfo> page = PageMybatisInterceptor.endPage();
            return page;
        }  catch (Throwable e) {
            LOG.error("fetchClientUpdateList exception",e,clientUpdateRequest);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取详情
     * @param id
     * @return
     * @throws DatabaseException
     */
    @Override
    public ClientUpdateInfo fetchClientUpdateDetail(Integer id) throws DatabaseException {
        try {
            if (id == null){
                LOG.error("fetchClientUpdateDetail 主键为空",id);
                return null;
            }
            ClientUpdateInfo clientUpdateInfo = clientUpdateInfoMapper.selectByPrimaryKey(id);
            return clientUpdateInfo;
        } catch (Throwable e) {
            LOG.error("fetchClientUpdateDetail 异常",e,id);
            throw new DatabaseException(e.getMessage());
        }
    }
}
